package org.jeecg.modules.pay.listener;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.boot.starter.rabbitmq.core.BaseRabbiMqHandler;
import org.jeecg.boot.starter.rabbitmq.listenter.MqListener;
import org.jeecg.common.annotation.RabbitComponent;
import org.jeecg.common.base.BaseMap;
import org.jeecg.modules.pay.entity.UserAccount;
import org.jeecg.modules.pay.service.IUserAccountService;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;

/**
 * @author OSFung
 * @date 2022-06-15 04:49
 */

@Slf4j
@RabbitListener(queues = "importUser")
@RabbitComponent(value = "importUserListener")
public class ResourceInitListener extends BaseRabbiMqHandler<BaseMap> {

    @Autowired
    private IUserAccountService userAccountService;

    @RabbitHandler
    public void onMessage(BaseMap baseMap, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        super.onMessage(baseMap, deliveryTag, channel, new MqListener<BaseMap>() {
            @Override
            public void handler(BaseMap map, Channel channel) {
                SysUser user = map.get("user");
                UserAccount userAccount = new UserAccount();
                BeanUtils.copyProperties(user, userAccount);
                userAccount.setUserName(user.getUsername());
                userAccountService.save(userAccount);
            }
        });
    }
}
